package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.AreaManagement;
import com.example.prison.entity.AreaRecord;
import com.example.prison.entity.DefenceArea;
import com.example.prison.entity.SystemInfo;
import com.example.prison.mapper.*;
import com.example.prison.service.AreaManagementService;
import com.example.prison.util.APIUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
* @author jiang
* @description 针对表【area_management】的数据库操作Service实现
* @createDate 2023-05-26 17:15:28
*/
@Service
public class AreaManagementServiceImpl extends ServiceImpl<AreaManagementMapper, AreaManagement>
        implements AreaManagementService {

    @Autowired
    private AreaManagementMapper areaManagementMapper;

    @Autowired
    private AlarmManagementMapper alarmManagementMapper;

    @Autowired
    private DefenceAreaMapper defenceAreaMapper;

    @Autowired
    private AreaRecordMapper areaRecordMapper;

    @Autowired
    private SystemInfoMapper systemInfoMapper;

    @Override
    public PageInfo<AreaManagement> getAreaRealtimeState(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<AreaManagement> areaList = areaManagementMapper.get();
//        Random r = new Random();
//        for (AreaManagement area : areaList) {
//            //查询设备信息
//            String[] b = {"正常", "短路", "断路", "触网", "关闭"};
//            area.setVoltage(String.valueOf(r.nextInt(2000)));
//            area.setCurrent(String.valueOf(r.nextInt(10)));
//            area.setAlarmStatus(b[r.nextInt(5)]);
////            area.setTime(new SimpleDateFormat("yyyy/M/d HH:mm:ss").format(new Date()));
//            //更新实时数据
//            String alarmOne = alarmManagementMapper.getOneLevel("一级").getVoltage();
//            String alarmTwo = alarmManagementMapper.getOneLevel("二级").getVoltage();
//            String alarmThree = alarmManagementMapper.getOneLevel("三级").getVoltage();
//            area.setAlarmLevel(APIUtil.CountAlarmLevel(area.getVoltage(),area.getRatedVoltage(),alarmOne,alarmTwo,alarmThree));
//            areaManagementMapper.updateArea(area, area.getId());
//            //记录数据
//            AreaRecord record = new AreaRecord();
//            record.setDefenseSection(area.getDefenseSection());
//            record.setDefenseSectionPoint(area.getDefenseSectionPoint());
//            record.setTime(new SimpleDateFormat("yyyy/M/d HH:mm:ss").format(new Date()));
//            record.setVoltage(area.getVoltage());
//            record.setCurrent(area.getCurrent());
//            record.setRatedVoltage(area.getRatedVoltage());
//            record.setAlarmStatus(area.getAlarmStatus());
//            record.setDeviceNumber(area.getDeviceNumber());
//            record.setSentryPost(area.getSentryPost());
//            record.setSentryAddress(area.getSentryAddress());
//            record.setAlarmLevel(area.getAlarmLevel());
//            areaRecordMapper.add(record);
//        }
        return new PageInfo<>(areaList);
    }

    @Override
    public PageInfo<AreaManagement> get(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(areaManagementMapper.get());
    }

    @Override
    public PageInfo<AreaManagement> getAllArea(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(areaManagementMapper.getAll());
    }

    @Override
    public PageInfo<AreaManagement> getSentryPostArea(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(areaManagementMapper.getSentryPost());
    }

    @Override
    public int add(AreaManagement area) {
        String defenseSection = area.getDefenseSection();
        String defenseSectionPoint = area.getDefenseSectionPoint();
        area.setStatus(1);
        if (ObjectUtils.isNotEmpty(areaManagementMapper.getOneByDefenseSection(defenseSection))) {
            return -1;
        } else {
            if(ObjectUtils.isNotEmpty(systemInfoMapper.one())) {
                SystemInfo systemInfo = systemInfoMapper.one();
                area.setGatewayAddress(systemInfo.getGatewayAddress());
                area.setGatewayPort(systemInfo.getGatewayPort());
            } else {
                return -2;
            }
            if(ObjectUtils.isEmpty(area.getModuleAddress())) {
                return -3;
            }
            DefenceArea defenceArea = new DefenceArea();
            defenceArea.setNumber(area.getDefenseSection());
            defenceArea.setAddress(area.getDefenseAddress());
            defenceArea.setStatus(area.getStatus());
            defenceAreaMapper.add(defenceArea);
            areaManagementMapper.add(area);
            return 1;
        }
    }

    @Override
    public int addSentryPost(AreaManagement area) {
        String sentryPost = area.getSentryPost();
        if (ObjectUtils.isNotEmpty(areaManagementMapper.getBySentryPost(sentryPost))) {
            return -1;
        } else {
            areaManagementMapper.add(area);
            return 1;
        }
    }

    @Override
    public Integer delete(Integer[] ids) {
        for (Integer id : ids ) {
            if (ObjectUtils.isEmpty(areaManagementMapper.getById(id))) {
                return -1;
            }
            AreaManagement area = areaManagementMapper.getById(id);
            if (ObjectUtils.isEmpty(defenceAreaMapper.getOneByNumber(area.getDefenseSection()))) {
                areaManagementMapper.deleteAreaBySection(area.getDefenseSection());
                return 0;
            }
            DefenceArea defenceArea = defenceAreaMapper.getOneByNumber(area.getDefenseSection());
            defenceAreaMapper.deleteDefenceAreaById(defenceArea.getId());
            areaManagementMapper.deleteAreaBySection(area.getDefenseSection());
        }
        areaManagementMapper.deleteArea(ids);
        return 1;
    }

    @Override
    public Integer deleteBySection(String defenseSection) {
        if (ObjectUtils.isEmpty(areaManagementMapper.getOneByDefenseSection(defenseSection))) {
            return -1;
        }
        if (ObjectUtils.isEmpty(defenceAreaMapper.getOneByNumber(defenseSection))) {
            areaManagementMapper.deleteAreaBySection(defenseSection);
            return 0;
        }
        DefenceArea defenceArea = defenceAreaMapper.getOneByNumber(defenseSection);
        defenceAreaMapper.deleteDefenceAreaById(defenceArea.getId());
        areaManagementMapper.deleteAreaBySection(defenseSection);
        return 1;
    }

    @Override
    public int updateArea(AreaManagement area, Integer id) {
        if(ObjectUtils.isEmpty(areaManagementMapper.getById(id))) {
            return 2;
        }
        AreaManagement areaDTO = areaManagementMapper.getById(id);
        areaManagementMapper.updateAreaName(area, id);
        List<AreaManagement> areaList = areaManagementMapper.getByDefenseSection(area.getDefenseSection());
        if(areaList.size() > 1) {
            areaManagementMapper.updateAreaName(areaDTO, id);
            return -1;
        } else {
            if(ObjectUtils.isEmpty(defenceAreaMapper.getOneByNumber(areaDTO.getDefenseSection()))) {
                return 0;
            }
            DefenceArea defenceArea = defenceAreaMapper.getOneByNumber(areaDTO.getDefenseSection());
            defenceArea.setNumber(area.getDefenseSection());
            defenceArea.setAddress(area.getDefenseAddress());
            defenceArea.setStatus(area.getStatus());
            defenceAreaMapper.updateDefenceArea(defenceArea, defenceArea.getId());
            return 1;
        }
    }
    @Override
    public int updateAreaName(AreaManagement area, Integer id) {
        areaManagementMapper.updateAreaName(area, id);
        return 1;
    }
    @Override
    public int updateAreaVol(AreaManagement area, Integer id) {
        areaManagementMapper.updateAreaVol(area, id);
        return 1;
    }

    @Override
    public AreaManagement getById(Integer id) {
        return areaManagementMapper.getById(id);
    }

    @Override
    public PageInfo<AreaManagement> getBySection(int pageNumber, int pageSize, String defenseSection) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(areaManagementMapper.getBySection(defenseSection));
    }

    @Override
    public PageInfo<AreaManagement> getByPoint(int pageNumber, int pageSize, String defenseSectionPoint) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(areaManagementMapper.getByPoint(defenseSectionPoint));
    }

    @Override
    public PageInfo<AreaManagement> getBySectionAndPoint(int pageNumber, int pageSize, String defenseSection, String defenseSectionPoint) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(areaManagementMapper.getLinkBySectionAndPoint(defenseSection, defenseSectionPoint));
    }

    @Override
    public PageInfo<AreaManagement> getBySentryPost(int pageNumber, int pageSize, String sentryPost) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(areaManagementMapper.getBySentryPost(sentryPost));
    }

    @Override
    public PageInfo<AreaManagement> getByAlarmStatus(int pageNumber, int pageSize, String alarmStatus) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(areaManagementMapper.getByStatus(alarmStatus));
    }

}




